﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MShop.Core.Domain;
using MShop.Core.DataAccess;
 

namespace MShop.Web.Areas.Admin.Controllers
{
    public class DictController : SecureController
    {
        private readonly IDictDao _DictDao;

        public DictController(IDictDao dictDao)
        {
            _DictDao = dictDao;
        }
        
        public ActionResult Index(string typeid)
        {
           
            IList<SelectListItem> list = _DictDao.ToSelectListItem(typeid);             
            if (string.IsNullOrEmpty(typeid))
            {
                typeid = list[0].Value;
            }            
            ViewData["dicts"] = list;

            return View(_DictDao.GetByTypeId(int.Parse(typeid)));
        }

        public ActionResult Create()
        {           
            return View(new Brand());
        }

        [HttpPost]
        public ActionResult Create(Brand brand)
        {
            
            if (ModelState.IsValid)
            {
                _DictDao.Save(brand);
                _DictDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(brand);
        }

        public ActionResult Edit(int id)
        {

            return View(_DictDao.GetById(id));
        }


        public ActionResult Delete(int id)
        {
            var brand = _DictDao.GetById(id);
            _DictDao.Delete(brand);
            _DictDao.SaveChanges();
            return RedirectToAction("index");
        }


        [HttpPost]
        public ActionResult Edit(int id, Dictionary brand)
        { 
            if (ModelState.IsValid)
            {
                var dbbrand = _DictDao.GetById(id);
                UpdateModel<Dictionary>(dbbrand, new string[] { "Name", "Icon", "Desc" });
                _DictDao.SaveChanges();
                return RedirectToAction("index");
            }
            return View(brand);

            
        }
    }
}
